package jp.co.kayo.gae.takidashibot.controller.rsscheck;

import jp.co.kayo.gae.takidashibot.service.RssCheck;
import jp.co.kayo.gae.takidashibot.utils.Consts;
import jp.co.kayo.gae.takidashibot.utils.TwitterClient;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.memcache.Memcache;

/***
 * 炊き出し情報の収集
 * この処理は５分程度間隔で呼ばれる処理です。
 * 一回の処理で、各県ひとつの処理を行います、数値はカウントアップされすべて一回ずつ
 * 実施されたら最初に戻ります。
 * 
 * @author yokmama
 *
 */
public class IndexController extends Controller {
    RssCheck service = new RssCheck();

    @Override
    public Navigation run() throws Exception {
        TwitterClient twitter = service.getTwitter(-1);
        Integer o = (Integer)Memcache.get("current.work");
        int cur = 0;
        if(o != null){
            cur = o.intValue();
        }
        try{
            if(cur == 0){
                service.getTakidasiInfo(twitter, "宮城", Consts.JSON_MIYAGI, Consts.S_MIYAGI, "#save_miyagi");
            }
            else if(cur == 1){
                service.getTakidasiInfo(twitter, "福島", Consts.JSON_FUKUSHIMA, Consts.S_FUKUSIMA, "#save_fukusima");
            }
            else if(cur == 2){
                service.getTakidasiInfo(twitter, "茨城", Consts.JSON_IBARAKI, Consts.S_IBARAKI, "#save_ibaraki");
            }
            else if(cur == 3){
                service.getTakidasiInfo(twitter, "栃木", Consts.JSON_TOCHIGI, Consts.S_TOCHIGI, "#save_tochigi");
            }
            else if(cur == 4){
                service.getTakidasiInfo(twitter, "青森", Consts.JSON_AOMORI, Consts.S_AOMORI, "#save_aomori");
            }
            else if(cur == 5){
                service.getTakidasiInfo(twitter, "岩手", Consts.JSON_IWATE, Consts.S_IWATE, "#save_iwate");
            }
            else if(cur == 6){
                service.getTakidasiInfo(twitter, "山形", Consts.JSON_YAMAGATA, Consts.S_YAMAGATA, "#save_yamagata");
            }
        }
        finally{
            cur++;
            if(cur > 6){
                cur = 0;
            }
            Memcache.put("current.work", cur);
        }
        
        return null;
    }
}
